package com.atom.common.activiti.diagram;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.atom.common.CommonConstant;
import com.atom.common.dao.CommentFlowDao;
import com.atom.common.entity.CommentFlowDO;
import com.atom.common.entity.bo.CallSubDiagram;
import com.atom.common.entity.bo.TaskDiagramBO;
/**
 * Title: CommentDiagramHandler
 * Description: 生成征求意见流程图
 * @author WEWE
 * @date 2016年10月21日 下午1:36:58
 * @version 1.0
 */
@Component
public class CommentDiagramHandler {
  
  @Resource
  private CommentFlowDao commentFlowDao;
  
  private static final String STATE = "state";
  
  public TaskDiagramBO getCommentDiagram (String taskDefinitionKey, String mainProcessInstanceId) {
    TaskDiagramBO diagram = new TaskDiagramBO(taskDefinitionKey);
    List<CommentFlowDO> tasks = commentFlowDao.findByMainPrcInsIdByTaskDefKey(mainProcessInstanceId, taskDefinitionKey);
    // 根据记录判断壳状态
    int state = getTaskState(tasks);
    diagram.setState(state);
    // 将所有根都置为zqyj
    tasks = setRootToZQYJ(tasks);
    // 创建征求意见树
    CallSubDiagram callSubDiagram = buildTree(tasks);
    diagram.setZqyj_datas(callSubDiagram);    
    return diagram;
  }

  
  
  private List<CommentFlowDO> setRootToZQYJ(List<CommentFlowDO> tasks) {
    Map<String, Object> keyMap = new HashMap<String, Object>();
    for (CommentFlowDO task : tasks) {
      keyMap.put(task.getAssignee(), null);
    }
    for (CommentFlowDO task : tasks) {
      if (! keyMap.containsKey(task.getCreator())) {
        task.setCreator(CommonConstant.COMMENT_DIAGRAM_ROOT);
      }
    }
    return tasks;
  }



  private int getTaskState(List<CommentFlowDO> tasks) {
    int state = 0;
    for (CommentFlowDO task : tasks) {
      state = state > task.getStatus() ? state : task.getStatus();
    }
    return state;
  }



  private CallSubDiagram buildTree(List<CommentFlowDO> list){
    Map<String, List<CallSubDiagram>> map = createMap(list);
    CallSubDiagram root = new CallSubDiagram();
    root.setId(CommonConstant.COMMENT_DIAGRAM_ROOT);
    root.setAssignee(CommonConstant.COMMENT_DIAGRAM_ROOT);
    root.setName("征求意见");
    root.setChildren(getTrees(root, map));
    return root;
}
  
  private Map<String, List<CallSubDiagram>> createMap(List<CommentFlowDO> list){
    Map<String, List<CallSubDiagram>> map = new HashMap<String, List<CallSubDiagram>>();
    for (CommentFlowDO diagram : list) {
        String creator = diagram.getCreator();
        if (map.containsKey(creator)) {
            List<CallSubDiagram> diagrams = map.get(creator);
            diagrams.add(create(diagram));
            map.put(creator,diagrams);
        } else {
            List<CallSubDiagram> diagrams = new ArrayList<CallSubDiagram>();
            diagrams.add(create(diagram));
            map.put(creator,diagrams);
        }               
    }
    return map;
}
  
  private CallSubDiagram create(CommentFlowDO o){
    CallSubDiagram diagram = new CallSubDiagram();
    diagram.setId(String.valueOf(o.getId()));
    diagram.setName(o.getAssigneeText());
    diagram.setAssignee(o.getAssignee());
    diagram.setData(STATE, o.getStatus());
    return diagram;
}
  
  private List<CallSubDiagram> getTrees(CallSubDiagram root, Map<String,List<CallSubDiagram>> map){
    List<CallSubDiagram> callSubDiagramList = new ArrayList<CallSubDiagram>();
    if(map.containsKey(root.getAssignee())){
        for(CallSubDiagram diagram : map.get(root.getAssignee())){
            diagram.setChildren(getTrees(diagram, map));
            callSubDiagramList.add(diagram);
        }
    }
    return callSubDiagramList;
}

}
